package com.java3_9;

public class PrintNumber {
    private int number;

    public PrintNumber() {

    }

    public PrintNumber(int number) {
        this.number = number;
    }

    public synchronized void printOddNum() {
        if (number % 2 == 0) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.notify();
            System.out.println(Thread.currentThread().getName() + "  " + number);
            number++;
        }
        try {
            this.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //防止另一份线程结束，但是该线程锁死的情况
        this.notify();
    }

    public synchronized void printEvenNum() {
        if (number % 2 == 1) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.notify();
            System.out.println(Thread.currentThread().getName() + "  " + number);
            number++;
        }
        try {
            this.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //防止另一份线程结束，但是该线程锁死的情况
        this.notify();
    }


    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}


